package com.course.controller;

import com.course.entity.ResultBody;
import com.course.service.QuestionnaireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

@RestController
@RequestMapping("/questionnaire")
@CrossOrigin(origins = "*")
public class QuestionnaireController {
    @Autowired
    private QuestionnaireService questionnaireService;
    //调查问卷模块(学生端）
    @RequestMapping("/addquestionnaire")
    public ResultBody addquestionnaire(@RequestBody Map<String, Object> map)
    {
        System.out.println(map);
        if(map.get("courseid")==null||map.get("answer")==null||map.get("coursepoint")==null||map.get("openquestion")==null)
        {
            return new ResultBody(404,"参数错误",false);
        }
        String id=map.get("courseid").toString();
        String coursepoint=map.get("coursepoint").toString();
        String openquestion=map.get("openquestion").toString();
        Map<String,Object>ansewr=(Map<String, Object>) map.get("answer");
        ResultBody result= null;
        for (Map.Entry<String,Object> entry : ansewr.entrySet()) {
            String key = entry.getKey();
            String object= entry.getValue().toString();
            result= questionnaireService.addquestionnaire(id,key,object,coursepoint,openquestion);
            if(result.getStatus()!=200)
            {
                return new ResultBody(500,"回答失败",false);
            }
        }
        return new ResultBody(200,"回答成功",true);
    }
    @RequestMapping("/displayquestionnaire")
    public ResultBody displayquestionnaire(@RequestBody Map<String,Object>map)
    {
        if(map.get("id")==null)
        {
            return new ResultBody(404,"缺少参数",false);
        }
        return questionnaireService.displayquestionnaire(map);
    }

}
